package com.itheima.demo1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.PropertyPermission;
@WebServlet("/studentServlet")
public class studentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String age = req.getParameter("age");
        String score = req.getParameter("score");
        Student stu = new Student(username,Integer.parseInt(age),Integer.parseInt(score));
        //将对象值传入文件
        String realPath = getServletContext().getRealPath("/student.txt");
        BufferedWriter bw = new BufferedWriter(new FileWriter(realPath,true));
        bw.write(stu.getName()+":"+String.valueOf(stu.getAge())+":"+String.valueOf(stu.getScore()));
        bw.newLine();
        bw.close();
        //设置定时跳转
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("添加成功,2秒后自动跳转...");
        resp.setHeader("Refresh","2;URL=index.html");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
